import { reqGoodsList } from '../../../api/goods'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [], // 商品列表数据
    total: 0,
    isFinish: false, // 判断数据是否加载完毕
    isLoading: false,

    requestData: {
      page: 1,
      limit: 10,
      category1Id: '',
      category2Id: ''
    }
  },
  async getGoodsList() {
    this.setData({
      isLoading: true
    })
    const { data } = await reqGoodsList(this.data.requestData)
    this.setData({
      isLoading: false
    })
    this.setData({
      goodsList: [...this.data.goodsList, ...data.records],
      total: data.total
    })
  },
  onReachBottom() {
    const { total, goodsList, isLoading } = this.data
    const { page } = this.data.requestData
    if (isLoading) {
      return
    }
    if (total === goodsList.length) {
      this.setData({
        isFinish: true
      })
      return
    }
    this.setData({
      requestData: { ...this.data.requestData, page: page + 1 }
    })
    this.getGoodsList()
  },
  onPullDownRefresh() {
    this.setData({
      goodsList: [],
      total: 0,
      isLoading: false,
      isFinish: false,
      requestData: { ...this.data.requestData, page: 1 }
    })
    this.getGoodsList()
  },
  onLoad(options) {
    this.setData({
      requestData: { ...this.data.requestData, ...options }
    })
    this.getGoodsList(this.data.requestData)
    wx.stopPullDownRefresh()
  },
  onShareAppMessage() {},
  onShareTimeline() {}
})
